<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate ? '修改账户归属客户' : '添加账户归属客户'"
    :visible="visible"
    custom-class="ele-dialog-form"
    width="520px"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="86px">
      <el-form-item label="归属客户:" prop="name">
        <el-input
          v-model="form.name"
          :maxlength="20"
          clearable
          placeholder="请输入账户归属客户名称"
        />
      </el-form-item>

      <el-form-item label="备注:" style="margin-bottom: 10px">
        <el-input
          v-model="form.remark"
          :maxlength="200"
          :rows="3"
          clearable
          placeholder="请输入备注信息"
          type="textarea"
        />
      </el-form-item>

      <el-form-item label="状态:">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button :loading="loading" type="primary" @click="save"
        >确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "customerEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
  },

  data() {
    return {
      form: Object.assign({}, this.data, {
        status: 1,
      }),

      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            type: "string",
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("请输入账户归属客户名称"));
              }
              this.$http
                .get("/customer/query?param=" + value)
                .then((res) => {
                  if (res.data.code !== 0 || !res.data.data.length) {
                    return callback();
                  }
                  if (
                    this.isUpdate &&
                    res.data.data[0].url === this.data.DomainUrl
                  ) {
                    return callback();
                  }
                  callback(new Error("账户归属客户名称已存在"));
                })
                .catch(() => {
                  callback();
                });
            },
          },
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },

  watch: {
    data() {
      if (Object.keys(this.data).length > 0) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        for (let i in this.form) {
          this.form[i] = null;
        }
        this.form.status = 1;
        this.isUpdate = false;
      }
    },
  },

  methods: {
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http
            .post(
              "/customer/" + (this.isUpdate ? "update" : "create"),
              this.form
            )
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                if (!this.isUpdate) {
                  for (let i in this.form) {
                    this.form[i] = null;
                  }
                  this.form.status = 1;
                }
                this.updateVisible(false);
                this.$emit("done", this.isUpdate);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },

    updateVisible(value) {
      this.$emit("update:visible", value);
    },
  },
};
</script>
